package com.uhu.code;

import java.util.*;

/**
 * 128. 最长连续序列
 * https://leetcode.cn/problems/longest-consecutive-sequence/?envType=study-plan-v2&envId=top-100-liked
 */
public class Solution128 {
    public int longestConsecutive(int[] nums) {
        if(nums == null || nums.length == 0) {
            return 0;
        }
        Set<Integer> setInt = new HashSet<>();
        for(int i = 0; i < nums.length; i++) {
            setInt.add(nums[i]);
        }

        List<Integer> newNumArray = new ArrayList<>();
        newNumArray.addAll(setInt);
        Collections.sort(newNumArray);

        List<Integer> longList = new ArrayList<>();
        int count = 1;
        for(int i = 0; i <= newNumArray.size() - 2; i++) {
            if(newNumArray.get(i+1) - newNumArray.get(i) == 1) {
                count++;
            }else {
                longList.add(count);
                count = 1;
            }
        }
        longList.add(count);
        Collections.sort(longList);
        return longList.get(longList.size() - 1);
    }
}
